#define _CRT_SECURE_NO_WARNINGS  1
#include <stdio.h>
void find(int ai, int(*arr2)[2], int m, int* arr1)
{
    int j = 0;
    int s = 0;
    for (j = 0; j < m; j++)
    {
        if (*(*(arr2 + j) + 0) == ai)
        {
            int i = 0;
            while (i < m)
            {
                if (arr1[i] == -1)
                {
                    arr1[i] = (*(*(arr2 + j) + 1));
                    find(*(*(arr2 + j) + 1), arr2, m, arr1);
                    break;
                }
                if (arr1[i] == *(*(arr2 + j) + 1))
                {
                    break;
                }
                i++;
            }
        }
        if (*(*(arr2 + j) + 1) == ai)
        {
            int i = 0;
            while (i < m)
            {
                if (arr1[i] == -1)
                {
                    arr1[i] = (*(*(arr2 + j) + 0));
                    find((*(*(arr2 + j) + 0)), arr2, m, arr1);
                    break;
                }
                if (arr1[i] == (*(*(arr2 + j) + 0)))
                {
                    break;
                }
                i++;
            }
        }
    }
}
int main() {
    int n = 0;
    int ai = 0;
    int m = 0;
    while (scanf("%d%d%d", &n, &ai, &m) != EOF)
    {
        int arr1[100];
        int k = 0;
        for (k = 0; k < n; k++)
        {
            arr1[k] = -1;
        }
        int arr2[100][2];
        int i = 0;
        for (i = 0; i < m; i++)
        {
            scanf("%d,%d", &arr2[i][0], &arr2[i][1]);
        }
        find(ai, arr2, m, arr1);
        int count = 0;
        int p = 0;
        while (arr1[p] != -1)
        {
            if (arr1[p] != ai)
            {
                count++;
            }
            p++;
        }
        int count1 = 0;
        for (i = 0; i < m; i++)
        {
            if (arr2[i][0] == ai || arr2[i][1] == ai)
            {
                count1++;
            }
        }
        count = count - count1;
        printf("%d", count);
    }
    return 0;
}